<?php

namespace app\admin\service;

use app\admin\model\BoxSetScore;
use DateTime;
use think\db\exception\BindParamException;
use think\exception\PDOException;

class BoxSetScoreService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxSetScore();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-07-27 17:28
     * @return array
     * @throws BindParamException
     * @throws PDOException
     */
    public function getList(): array
    {
        $param = request()->param();
        $limit = $param['limit'] ?? 10;
        $map   = [];

        if (isset($param['time_range'][0]) && !empty($param['time_range'][0])) {
            $map[]               = ['start_time', '<=', $param['time_range'][0]];
            $map[]               = ['end_time', '>=', $param['time_range'][0]];
            $param['start_time'] = $param['time_range'][0];
        }
        if (isset($param['time_range'][1]) && !empty($param['time_range'][1])) {
            $param['end_time'] = $param['time_range'][1];
        }

        if (!empty($param['time_range'])) {

                $sql = "SELECT * FROM cq_box_set_score WHERE (`start_time` <= '" . $param['start_time'] . "' AND `end_time` >= '" . $param['start_time'] . "')
            OR (`start_time` <= '" . $param['end_time'] . "' AND `end_time` >= '" . $param['end_time'] . "')
            OR (`start_time` >= '" . $param['start_time'] . "' AND `end_time` <= '" . $param['end_time'] . "')
            ORDER BY id DESC LIMIT ". PERPAGE;
                $list = $this->model->query($sql);
                $sql1 = "SELECT count(*) as num FROM cq_box_set_score WHERE (`start_time` <= '" . $param['start_time'] . "' AND `end_time` >= '" . $param['start_time'] . "')
            OR (`start_time` <= '" . $param['end_time'] . "' AND `end_time` >= '" . $param['end_time'] . "')
            OR (`start_time` >= '" . $param['start_time'] . "' AND `end_time` <= '" . $param['end_time'] . "')
            ORDER BY id";
                $list1 = $this->model->query($sql1);
                $count = $list1[0]['num'];
        } else {
            $info  = $this->model
                ->where($map)
                ->order('id desc')->paginate($limit)->toArray();
            $list  = $info['data'];
            $count = $info['total'];
        }

        foreach ($list as $key => &$value) {
            $list[$key]['time_range'] = $value['start_time'] . ' ~ ' . $value['end_time'];
        }
        //返回结果
        return array (
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        );
    }


    function isDateCrossMonth($date1, $date2)
    {
        $date1 = new DateTime($date1);
        $date2 = new DateTime($date2);

        $month1 = $date1->format('m');
        $month2 = $date2->format('m');

        return $month1 != $month2;
    }

    //审核
    public function edit()
    {
        $param               = request()->param();
        $param['start_time'] = $param['time_range'][0] ?? '';
        $param['end_time']   = $param['time_range'][1] ?? '';

        //判断不能跨月份设置
        $res = $this->isDateCrossMonth($param['start_time'], $param['end_time']);
        if ($res) {
            return [
                "msg"  => '日期出现跨月份,无法设置!',
                "code" => -1,
                "data" => "",
            ];
        }

        if (empty($param['id'])) {
            $sql = "SELECT id FROM cq_box_set_score WHERE (`start_time` <= '" . $param['start_time'] . "' AND `end_time` >= '" . $param['start_time'] . "') OR (`start_time` <= '" . $param['end_time'] . "' AND `end_time` >= '" . $param['end_time'] . "')";
            $res = $this->model->query($sql);
            if (!empty($res)) {
                return [
                    "msg"  => '当前时间段已有天数被设置!',
                    "code" => -1,
                    "data" => "",
                ];
            }
            $param['create_time'] = date('Y-m-d H:i:s');
        }

        return parent::edit($param); // TODO: Change the autogenerated stub
    }


    public function status()
    {
        return parent::status(); // TODO: Change the autogenerated stub
    }

}